package com.atbeijing.D02;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;


/**
 * map
 * 针对流中的元素,一对一的转换
 */
public class Example2 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<SensorReading> source = env.addSource(new SensorSource());

        source.map(new MapFunction<SensorReading, String>() {
            @Override
            public String map(SensorReading sensorReading) throws Exception {
                return sensorReading.id;
            }
        }).print();

        // 使用flatMap来实现Map的功能
        source.flatMap(new FlatMapFunction<SensorReading,String>() {

            @Override
            public void flatMap(SensorReading sensorReading, Collector<String> collector) throws Exception {
                collector.collect(sensorReading.id);
            }
        }).print();




        env.execute();
    }
}
